// pages/user/info/index.js
const App = getApp();

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'



Page({

    /**
     * 页面的初始数据
     */
    data: {
        nickname:'',
        value:'',
        avatarUrl : defaultAvatarUrl,
        selectAvatar: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserDetail();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
    onSaveAvatarNickname(e){
        console.log(e,'form');
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    /**
   * 获取当前用户信息
   */
    getUserDetail() {
        let _this = this;
        App._get('user.index/detail', {}, function (result) {
            _this.setData({
                avatarUrl : result.data.userInfo.avatarUrl,
                nickname : result.data.userInfo.nickName,
            });
        });
    },
    onNickNameChange(e){
        this.setData({
            nickname : e.detail.value
        })
    },
    onSubmit(){
        let nickname = this.data.nickname;
        if(!nickname){
            App.showError('请输入合法的昵称！');
            return false;
        }
        let params = {};
        params.avatarUrl = this.data.avatarUrl;
        params.nickname = this.data.nickname;
        App._get('user.index/update',params,function(res){
            if(res.code == 1){
                App.showSuccess(res.msg)
            }
        })
    },
    onChooseAvatar(e) {
        const { avatarUrl } = e.detail 
        let _this = this;
        // POST 参数
        let params = {
            type:'avatar',
            wxapp_id: App.getWxappId(),
            token: wx.getStorageSync('token')
        };
        wx.uploadFile({
            url: App.api_root + 'upload/image',
            filePath: avatarUrl,
            name: 'iFile',
            formData: params,
            success: function(res) {
              let data = res.data;
              let respone = JSON.parse(data);
              if(respone.code == 1){
                _this.setData({
                    selectAvatar : 1,
                    avatar_id : respone.data.file_id,
                    avatarUrl : respone.data.file_path,
                })
              }
            },
            complete: function() {
              console.log('complete')
            }
        });
    },
})